<?php

if (!defined('BASEPATH'))
	exit('No Direct Access Allowed !');


class hewan extends CI_Controller
{
	
	function __construct()
	{

		parent::__construct();
		$this->load->model('m_hewan');
		$this->load->library('Excel');

	}

	

	function index(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$data['data'] = $this->m_hewan->ambil();
			$this->load->view('global/header');
			$this->load->view('hewan/index', $data);
			$this->load->view('global/footer');
		}
	}

	function cetak_excel(){
		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel();

		// Set document properties
		$objPHPExcel->getProperties()->setCreator("Braska Jaya")
		->setLastModifiedBy("Braska Jaya")
		->setTitle("Data Hewan Kurban Kabupaten Tapin")
		->setSubject("Data Hewan Kurban Kabupaten Tapin")
		->setDescription("Data Hewan Kurban Kabupaten Tapin. Dibuat oleh Braska Jaya")
		->setKeywords("Hewan Kurban Tapin")
		->setCategory("Hewan Kurban");

		$datar = $this->m_hewan->ambil2();
		//print_r($datar);
		$attendacne_data[] = array("Nama Pemilik", "Jenis", "Berat", "Umur", "Tanggal Periksa", "Pemeriksa" ); 
		// Add some data, 
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A1', 'Data Hewan Kurban Kabupaten Tapin')
		->setCellValue('A3', 'Nama Pemilik')
		->setCellValue('B3', 'Jenis')
		->setCellValue('C3', 'Berat')
		->setCellValue('D3', 'Umur')
		->setCellValue('E3', 'Pemeriksa')
		->setCellValue('F3', 'Tanggal Periksa');

		$objPHPExcel->getActiveSheet()->setTitle('Data Hewan Kurban');

		$row = 4;
		for ($i=0; $i < count($datar); $i++) { 
			if($datar[$i]->jenis_hewan == 1){
				$datar[$i]->jenis_hewan = 'Sapi';
			}
			if($datar[$i]->jenis_hewan == 2){
				$datar[$i]->jenis_hewan = 'Kambing';
			}
			if($datar[$i]->jenis_hewan == 3){
				$datar[$i]->jenis_hewan = 'Domba';
			}
			foreach ($attendacne_data as $data) {
				$col = 0;
				$row++;
				foreach ($datar[$i] as $field_val) {
					$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col++, $row, $field_val);	
				}
			}
			
		}

		//this is the header given from PHPExcel examples. but the output seems somewhat corrupted in some cases.
		//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		//so, we use this header instead.
		$objPHPExcel->setActiveSheetIndex(0);
		ob_end_clean();
		header('Content-type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename=Data Hewan.xlsx');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('php://output');
	}


	function hapus($id='') {
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$this->m_hewan->hapus($id);
			redirect('hewan');
		}
	}


}

